package com.mock.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mock.dao.AddressDao;
import com.mock.model.Address;
import com.mock.model.DataTable;
import com.mock.utils.HibernateUtil;

public class AddressDaoImp extends GenericDaoImpl<Address, Long> implements AddressDao{

	@Override
	public List<Address> getAllAddress() { 
		return super.getAll(Address.class);
	}

	@Override
	public Address getById(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Address address = (Address)session.load(Address.class, id);
		if(null != address)
			return address;
		return null;
	}

	@Override
	public Address getByPostcode(String postcode) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from Address a where a.postcode =:postcode");
		query.setParameter("postcode", postcode);
		return (Address)query.uniqueResult();
	}

	@Override
	public Address getByStreet(String addressName) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from Address a where a.addressName =:addressName");
		query.setParameter("addressName", addressName);
		return (Address)query.uniqueResult();
	}


	@Override
	public Address getByName(String nameColume, String valueSearch) {
		
		return super.getByColumn(Address.class, nameColume, valueSearch);
	}
	

	@Override
	public List<Address> getAddressByQuery(DataTable dataTable) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		// alias
		Criteria criteria = session.createCriteria(Address.class, "address");
		// write alias join table country
		criteria.createAlias("address.country", "country");
		criteria.setFetchMode("address.country", FetchMode.JOIN);
		// write alias join table county
		criteria.createAlias("address.county", "county");
		criteria.setFetchMode("address.county", FetchMode.JOIN);
		// write alias join table town
		criteria.createAlias("address.town", "town");
		criteria.setFetchMode("address.town", FetchMode.JOIN);
		// get sort implement follow column
		String columnName = "id";
		switch (dataTable.iSortCol_0) {
		case 0:
			columnName = "id";
			break;
		case 1:
			columnName = "addressName1";
		case 2:
			columnName = "addressName2";
		case 3: 
			columnName = "addressName3";
		case 4: 
			columnName = "postcode";
		case 5: 
			columnName = "town.townName";
		case 6: 
			columnName = "county.countyName";
		case 7:
			columnName = "country.countryName";
		default:
			break;
		}
		//search follow content from input text...
		if(dataTable.sSearch_4 == null && dataTable.sSearch_5 == null && dataTable.sSearch_1 == null){
			
		}
		else{
			criteria.add(Restrictions.ilike("addressName1", dataTable.sSearch_1 + "%"));
			criteria.add(Restrictions.ilike("postcode", dataTable.sSearch_4 + "%"));
			criteria.add(Restrictions.ilike("town.townName", dataTable.sSearch_5 + "%"));
		}
		
		// get sort direct follow column..
		if ("asc".equalsIgnoreCase(dataTable.sSortDir_0)
				) {
			criteria.addOrder(Order.asc(columnName));
		} else {
			criteria.addOrder(Order.desc(columnName));
		}
		// Paging
		criteria.setFirstResult(dataTable.iDisplayStart);
		criteria.setMaxResults(dataTable.iDisplayLength);
		
		List<Address> addresses = criteria.list();
		session.getTransaction().commit();
		return addresses;

	}

	@Override
	public int getRowCount() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		int count = ((Long) session.createQuery("select count(*) from Address")
				.uniqueResult()).intValue();
		session.getTransaction().commit();
		return count;
	}

	public static void main(String[] args){
		AddressDaoImp run = new AddressDaoImp();
		DataTable dataTable = new DataTable();
		dataTable.sSearch_1 = "Address 1";
		List<Address> list = run.getAddressByQuery(dataTable);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i).getPostcode());
		}
	}

	

	

}
